package source;

/*
 * Author: Aqeel Kazmi
 * Email: aqeelhkazmi@gmail.com
*/

import javax.media.*;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.net.*;

import javax.swing.*;

import java.awt.*;
import java.awt.event.*;
import javax.media.bean.playerbean.*;

public class MediaPlayerFrame extends JFrame {	
	
	Frame fullScreenFrame = null;
		
    static final String FRAME_TITLE = "My Media Player";
    //String songs[]=null;
    
	public boolean loops = false;
	public boolean fullScreen = false;	

    private static final int LOC_X = 300;
    private static final int LOC_Y = 150;
    private static final int HEIGHT = 430;
    private static final int WIDTH = 620;
    public	ShowFiles sf = null;
    public RemoteFile rf = null;
    
    JPanel backGround = null;
	JPanel controlButtons = null;
	JPanel displayAndControl = null;
	JPanel mainPanel = null;
	
	   
    private MediaPlayer player = null;
    private Container con = null;
	
    MediaMenu  mainmenu = null;
    ControlPanel controlPanel = null;
    
    public MediaPlayerFrame() {
    	
        super(FRAME_TITLE);
        setLocation(LOC_X, LOC_Y);
        setSize(WIDTH, HEIGHT);
        //fullScreenFrame.setBounds()
	    setLayout(new GridLayout());
	    setFocusable(true);
        setFocusCycleRoot(true);
        setFocusTraversalKeysEnabled(true);
	
        backGround = new JPanel();
        backGround.setBackground(Color.BLACK);
        backGround.setLayout(new GridLayout());
        Icon ii = new ImageIcon("images/back.jpg");
        JLabel jl = new JLabel(ii);
        
        backGround.setFocusable(false);
        backGround.add(jl);
        sf = new ShowFiles(this);
        rf = new RemoteFile(this);
    	mainmenu = new MediaMenu(this);
	    
    	addKeyListener(mainmenu.ml);	
    
    	setDefaultCloseOperation(EXIT_ON_CLOSE);
        
        controlButtons = new JPanel();
        controlButtons.setLayout(new BorderLayout());
        
        mainPanel = new JPanel();
        mainPanel.setLayout(new GridLayout());
        mainPanel.addKeyListener(new KeysListen(this));
        mainPanel.add(createMainPanel());         
        
        con = getContentPane();
        con.add(mainPanel);
        con.addKeyListener(new KeysListen(this));                      
        //setResizable(false);        
        setVisible(true);        

    }    

    public void closeProcessEvent()
    {
    	WindowEvent we = new WindowEvent(this,WindowEvent.WINDOW_CLOSING);
    	processWindowEvent(we);
    	sf.close();
    }      

	public void kprocessEvent(KeyEvent ke)
	{
		processKeyEvent(ke);
	}
    
   
    public MediaPlayer getPlayer()
    {
    	return player;
    }
  
    private JPanel createMainPanel() {        
          
        displayAndControl = new JPanel();                
        displayAndControl.setLayout(new BorderLayout());   
        controlPanel = new ControlPanel(this);
        controlButtons.add(controlPanel.incom(),BorderLayout.SOUTH);    			
        controlButtons.add(controlPanel.incom2(),BorderLayout.NORTH);	
        displayAndControl.add(backGround,BorderLayout.CENTER);
        displayAndControl.add(controlButtons,BorderLayout.SOUTH);
        player = new MediaPlayer();
        ControlListen cl = new ControlListen(this);	
        player.addControllerListener(cl);
		player.realize();

		controlButtons.setFocusable(true);
		mainPanel.setFocusable(true);
		displayAndControl.setFocusable(true);	

		return displayAndControl;
    }
	  
    String GetFile()
		{
		
		String ret=null;
		FileDialog fd = new FileDialog(this,"Open File",FileDialog.LOAD);
		fd.setDirectory("");
		fd.setVisible(true);
		if (fd.getFile() != null) {
		ret = fd.getDirectory()+ fd.getFile();
		}
		return ret;
		}
	
	public void setLocator(MediaLocator locator)
	{		
			try{
				player.stop();
				player.close();
			}
			catch(Exception e)
			{	
			}
			mainPanel.setFocusable(true);
			
			try{								
			player.setMediaLocator(locator);
			getPlayer().prefetch();				
        }
        catch(Exception e)
        {
        	this.setTitle(FRAME_TITLE);
        	JOptionPane.showMessageDialog(null,"Unable to play this file","Error",JOptionPane.PLAIN_MESSAGE);        	
        	System.out.println(e);
        	return;
        }       	
                
    }

   
    public static void main(String[] args) {
        try {
               MediaPlayerFrame mpf = new MediaPlayerFrame();              
            
            }
        catch (Exception e) {
            e.printStackTrace();
        }
    }
}